import { productDetailById } from "../../../api/class"
import { openServer, shareAppMessageSet } from "../../../utils/config"
import { dataCollection } from "../../../utils/dataCollection"
import { swiperProps } from "../../../utils/type"

let key = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    productInfo: { id: '', },
    swiperList: <swiperProps[]>[],
    className: '',
    title: '',
    pageReady: false,
    themeCode:''
  },

  productId: '',
  productName: '',
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    this.productId = options.productId
    key = options.className + '-' + options.productName
   
    //从本地获取当前选择的城市编码
    this.setData({
      themeCode:wx.getStorageSync('themeCode')
    })

    productDetailById(options.productId).then(res => {
      const arr: swiperProps[] = []
      res.result.productPic.forEach((item: any) => {
        const obj: swiperProps = {
          type: 'image',
          url: ''
        }
        if ('picUrl' in item) {
          obj.type = 'image'
          obj.url = item.picUrl
        } else {
          obj.type = 'video'
          obj.url = item.videoUrl
          obj.poster = item.coverUrl
        }
        arr.push(obj)
      })
      this.setData({
        productInfo: res.result,
        swiperList: arr,
        className: options.className,
        title: res.result.productName,
        pageReady: true
      })
      this.productName = res.result.productName

    })
  },
  //获取产品详情
  getProductDetail() {

  },
  onOpenPage(res: any) {
    wx.setStorageSync('product', this.data.productInfo)
    wx.navigateTo({ url: res.currentTarget.dataset.path + '?productId=' + this.data.productInfo.id + '&className=' + this.data.className, })
  },
  onOpenServer() {
    //打开客服，带气泡
    openServer({
      showMessageCard: true,
      sendMessageTitle: '你好，我想咨询一下，' + this.data.className + '-' + this.productName,
      sendMessagePath: `/pages/class/product/product.html?productId=${this.productId}&className=${this.data.className}`,
      sendMessageImg: this.data.swiperList[0].url
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() }
  },
  onHide() {
    dataCollection.setPageMapData('leave')
    dataCollection.setComprehensiveMapData(key, 'leave', 'product')
  },
  onShow() {
    dataCollection.setPageMapData('enter', '学车类型')
    dataCollection.setComprehensiveMapData(key, 'enter', 'product')
  },
  onUnload() {
    dataCollection.setPageMapData('leave')
    dataCollection.setComprehensiveMapData(key, 'leave', 'product')
  },
})